//
//  NSDate+BirthDay.h
//  
//
//  Created by 罗亮富 on 12-12-6.
//  Copyright (c) 2012年  All rights reserved.
//

#import <Foundation/Foundation.h>

#define DATE_FORMAT_DEFAULT     @"yyyy-MM-dd"
#define TIME_FORMAT_DEFAULT     @"yyyy-MM-dd HH:mm:ss"
#define TIME_FORMAT_SEC         @"HH:mm:ss"

typedef struct date{
    unsigned char month;
    unsigned char date;
} DateType;

typedef enum {
    DateFormatHintNone,
    DateFormatHintRFC822,
    DateFormatHintRFC3339
} DateFormatHint;


typedef enum {
    time_unit_week,
    time_unit_hour,
    time_unit_day,
    time_unit_month,
    time_unit_year,
    time_unit_era,
    time_unit_None
}time_unit_type;


@interface NSDate (Utility)

@property (nonatomic, readonly) NSInteger year; ///< Year component
@property (nonatomic, readonly) NSInteger month; ///< Month component (1~12)
@property (nonatomic, readonly) NSInteger day; ///< Day component (1~31)
@property (nonatomic, readonly) NSInteger hour; ///< Hour component (0~23)
@property (nonatomic, readonly) NSInteger minute; ///< Minute component (0~59)
@property (nonatomic, readonly) NSInteger second; ///< Second component (0~59)
@property (nonatomic, readonly) NSInteger nanosecond; ///< Nanosecond component
@property (nonatomic, readonly) NSInteger weekday; ///< Weekday component (1~7, first day is based on user setting)
@property (nonatomic, readonly) NSInteger weekdayOrdinal; ///< WeekdayOrdinal component
@property (nonatomic, readonly) NSInteger weekOfMonth; ///< WeekOfMonth component (1~5)
@property (nonatomic, readonly) NSInteger weekOfYear; ///< WeekOfYear component (1~53)
@property (nonatomic, readonly) NSInteger yearForWeekOfYear; ///< YearForWeekOfYear component
@property (nonatomic, readonly) NSInteger quarter; ///< Quarter component
@property (nonatomic, readonly) BOOL isLeapMonth; ///< whether the month is leap month
@property (nonatomic, readonly) BOOL isLeapYear; ///< whether the year is leap year
@property (nonatomic, readonly) BOOL isToday; ///< whether date is today (based on current locale)
@property (nonatomic, readonly) BOOL isYesterday; ///< whether date is yesterday (based on current locale)
@property (nonatomic, readonly) BOOL isCurrentWeek; ///is the same week for current
@property (nonatomic, readonly) BOOL isAM; ///is the morning
@property (nonatomic, readonly) BOOL isLastDayInMonth; ///是否是这个月的最后一天

-(NSInteger)ageAsBirthDay;
-(NSString *)constellationAsBirthDay;

//比较日历时间日期跨度
-(BOOL)isOtherDayFromDate:(NSDate *)otherDay;
-(BOOL)isOtherMonthFromDate:(NSDate *)otherDay;
-(BOOL)isOtherYearFromDate:(NSDate *)otherDay;

-(NSString*)dateStringFromNow;
-(NSString*)timeStringFromNowIsShowDetail:(BOOL)isShowDetail;  //addByJida
+(NSDate *)dateFromFormatedString:(NSString *)formatString;
+(NSDate *)dateFromFormatedString:(NSString *)formatString byFormat:(NSString *)format;

-(NSString *)dateStringWithFormat:(NSString *)formatString;

+(NSString *)timeDateFormatter:(NSDate *)date type:(int)_type;

-(NSString *)appDateDescription;

-(NSString *)defaultDescription;

-(NSString *)referencedDateDescription;
-(NSDateComponents *)datePart;
-(NSDateComponents *)timePart;

//动态时间(发布的动态上显示的时间)
-(NSString*)dynamicTimeString;

//收藏时间(话题、游记、动态、活动的收藏时间)
-(NSString*)favoriteTimeString;

//当月一共有多少天
- (NSUInteger)daysOfMonth;

//属于一年中的第几周
-(NSUInteger)weekthOfTheYear;

/**
 *  比较两个日期, 日期为nil时,相当于0
 *
 */
+(NSComparisonResult)compareBetweenDate:(NSDate *)date1 andDate:(NSDate *)date2;

+ (NSInteger)daysBetweenDate:(NSDate*)fromDateTime andDate:(NSDate*)toDateTime;

/** 获取距离当前日期多少个月后的日期
 offset:偏离当前月多少个月
 */
- (NSDate *)dateOffsetByMonth:(NSInteger)offset;


//今日凌晨
+(NSDate *)today;
+(NSDate *)todayByOffset:(NSInteger)offset;

//某天凌晨
+(NSDate *)todayByDate:(NSDate *)date;

+ (NSDateComponents *)componetsWithTimeInterval:(NSTimeInterval)timeInterval;
//传入是相对当前时间的一个时间间隔  hasSecend 是否包含秒数
+ (NSString *)timeDescription1OfTimeInterval:(NSTimeInterval)timeInterval hasSecend:(BOOL)hasSecend;
+ (NSString *)timeDescriptionOfTimeInterval:(NSTimeInterval)timeInterval;
+ (NSString *)timeDescription1OfTimeInterval:(NSTimeInterval)timeInterval;




//传入过去某个aDate ，type是你需要得到的数据类型，aDate到现在的总 天数，月数，年数,星期数
-(NSInteger)countOfUnitSince1970ByUnitType:(time_unit_type)type;

-(NSInteger)unitIndexSinceDate:(NSDate *)refDate WithType:(time_unit_type)type;

-(NSInteger)unitIndexSince2010WithType:(time_unit_type)type;

+(NSArray *)beginAndEndDateOfUnitSince2010:(NSInteger)unitAmount inUnit:(NSCalendarUnit)unit;

+(NSDate *)the2010Year;


//传入 多少 秒，得到分钟，或小时单位的 NSString
+ (NSString*)getTimeStringWithTime:(NSInteger)time;
+ (NSString*)getTimeStringDetailWithTime:(NSInteger)time; //得到具体显示的时长

+ (NSDateComponents*)getDateComponentsWithDate:(NSDate*)date;
//传入秒，获得 多少 月 天 时 分
+ (NSString *)getFormartTimeStringWith:(NSInteger)sec;

- (NSString *)dailyString;


+ (long long)currentTimeStamp;
+ (NSString*)identifierOfTimeStamp;
+ (long long)iosTime:(long long)time;
+ (long long)serverTime:(long long)time;


//一天的开始时间0：0：0
- (NSDate*)startTimeOfADay;
//一天的结束时间23:59:59
- (NSDate*)endTimeOfADay;

// 获取月份描述
+ (NSString *)monthDescByMonth:(NSUInteger)index;

// Get date from RFC3339 or RFC822 string
// - A format/specification hint can be used to speed up,
//   otherwise both will be attempted in order to get a date
+ (NSDate *)dateFromInternetDateTimeString:(NSString *)dateString
                                formatHint:(DateFormatHint)hint;

// Get date from a string using a specific date specification
+ (NSDate *)dateFromRFC3339String:(NSString *)dateString;
+ (NSDate *)dateFromRFC822String:(NSString *)dateString;

@end

@interface NSDate (ServerTime)

+ (NSDate *)serverTime;

+ (BOOL)fixedServerTimeFromHttpResponse:(NSHTTPURLResponse *)response;

+ (BOOL)fixedServerTime:(int64_t)timestamp;

@end
